package leecode

import (
	"math"
	"sort"
)

func smallestDifference(a []int, b []int) int {
	//先排序
	sort.Ints(a)
	sort.Ints(b)
	i, j := 0, 0
	result := math.MaxInt
	for i < len(a) && j < len(b) {
		result = min(result, abs1606(a[i]-b[j]))
		if a[i] > b[j] {
			j++
		} else if b[j] > a[i] {
			i++
		} else {
			i++
			j++
		}
	}
	return result
}

func abs1606(a int) int {
	if a < 0 {
		return -a
	}
	return a
}
